/*************************************************************************
	> File Name: week16-2.msg3.c
	> Author: Potter
	> Mail: tobewhatwewant@gmail.com 
	> Created Time: 2014年06月18日 星期三 15时15分44秒
 ************************************************************************/

#include<stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>

struct msgbuf {
    long mtype;
    char mtext[1024];
};

int main(int argc, char * argv[]) {
	int msqid = 65537;
    key_t key = 8;
    struct msgbuf receive;
    while((msqid = msgget(key, IPC_CREAT | 0600)) == -1) {
        perror("Msgget");
        exit(-1);
    }
    
    while( msgrcv(msqid, &receive, sizeof(receive.mtext), 8, 0) > 0) {
        fprintf(stdout, "%s\n",receive.mtext);
    }

    msgctl(msqid, IPC_RMID, NULL);

	return 0;
}
